import { EventHandle } from '@/event-bus/types'

const eventHandles = new Map()

export default {
    on: (eName : string, eHandle : EventHandle) => {
        if (eventHandles.has(eName)) {
            //注册多处理器
            const handles = eventHandles.get(eName) as EventHandle[]
            handles.push(eHandle)
        } else {
            eventHandles.set(eName, [ eHandle ])
        }
    },
    publish: (eName : string, ...eArgs : unknown[]) => {
        if (!eventHandles.has(eName)) return
        const handles = eventHandles.get(eName) as EventHandle[]
        handles.forEach(h => h(eArgs))
    }
}
